package com.yxx.business.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import com.yxx.business.entity.TbEngineeringEditDto;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import com.yxx.common.annotation.Log;
import com.yxx.common.core.controller.BaseControllerPlus;
import com.yxx.common.enums.BusinessType;
import com.yxx.business.entity.TbEngineeringDo;
import com.yxx.business.entity.TbEngineeringVo;
import com.yxx.business.entity.TbEngineeringQueryDto;
import com.yxx.business.entity.TbEngineeringEditDto;
import com.yxx.business.service.ITbEngineeringService;
import com.yxx.common.utils.poi.ExcelUtil;
import com.yxx.common.core.domain.R;
import com.yxx.common.core.domain.PageResult;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springdoc.api.annotations.ParameterObject;

import org.springframework.validation.annotation.Validated;


/**
 * 工程信息Controller
 *
 * @author yxx
 * @date 2025-08-03
 */
@Tag(name = "工程信息管理-TbEngineering")
@RestController
@RequestMapping("/business/engineering")
@RequiredArgsConstructor(onConstructor_ = {@Lazy, @Autowired})
public class TbEngineeringController extends BaseControllerPlus {

    private final ITbEngineeringService tbEngineeringService;

    @Operation(summary = "查询--工程信息列表")
    @PreAuthorize("@ss.hasPermi('business:engineering:list')")
    @GetMapping("/list")
    public PageResult<TbEngineeringVo> list(@ParameterObject TbEngineeringQueryDto tbEngineeringQueryDto) {
        return tbEngineeringService.selectTbEngineeringPage(tbEngineeringQueryDto);
    }

    @Operation(summary = "查询--工程信息单个")
    @PreAuthorize("@ss.hasPermi('business:engineering:query')")
    @GetMapping(value = "/{engineeringId}")
    public R<TbEngineeringVo> getInfo(@PathVariable Long engineeringId) {
        return R.ok(tbEngineeringService.selectTbEngineeringByEngineeringId(engineeringId));
    }

    @Operation(summary = "新增--工程信息")
    @PreAuthorize("@ss.hasPermi('business:engineering:add')")
    @Log(title = "工程信息", businessType = BusinessType.INSERT)
    @PostMapping
    public R<String> add(@RequestBody @Validated TbEngineeringEditDto tbEngineeringQueryDto) {
        return toResult(tbEngineeringService.insertTbEngineering(tbEngineeringQueryDto));
    }

    @Operation(summary = "修改--工程信息")
    @PreAuthorize("@ss.hasPermi('business:engineering:edit')")
    @Log(title = "工程信息", businessType = BusinessType.UPDATE)
    @PutMapping("/{engineeringId}")
    public R<String> update(@PathVariable Long engineeringId, @RequestBody @Validated TbEngineeringEditDto tbEngineeringQueryDto) {
        return toResult(tbEngineeringService.updateTbEngineering(engineeringId, tbEngineeringQueryDto));
    }

    @Operation(summary = "删除--工程信息")
    @PreAuthorize("@ss.hasPermi('business:engineering:remove')")
    @Log(title = "工程信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{engineeringIds}")
    public R<String> delete(@PathVariable List<Long> engineeringIds) {
        return toResult(tbEngineeringService.deleteTbEngineeringByEngineeringIds(engineeringIds));
    }

    @Operation(summary = "导出--工程信息列表")
    @PreAuthorize("@ss.hasPermi('business:engineering:export')")
    @Log(title = "工程信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @ParameterObject TbEngineeringQueryDto tbEngineeringQueryDto) {
        List<TbEngineeringVo> list = tbEngineeringService.selectTbEngineeringVoList(tbEngineeringQueryDto);
        ExcelUtil<TbEngineeringVo> util = new ExcelUtil<>(TbEngineeringVo.class);
        util.exportExcel(response, list, "工程信息数据");
    }

    @Operation(summary = "导出--工程信息模板")
    @PreAuthorize("@ss.hasPermi('business:engineering:import')")
    @PostMapping("/importTemplate")
    public void exportTemplate(HttpServletResponse response) {
        ExcelUtil<TbEngineeringVo> util = new ExcelUtil<>(TbEngineeringVo.class);
        util.importTemplateExcel(response, "导入工程信息模板数据");
    }

    @Operation(summary = "导入--工程信息列表")
    @PreAuthorize("@ss.hasPermi('business:engineering:import')")
    @Log(title = "工程信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public R<Boolean> importData(MultipartFile file) throws IOException {
        ExcelUtil<TbEngineeringDo> util = new ExcelUtil<>(TbEngineeringDo.class);
        List<TbEngineeringDo> list = util.importExcel(file.getInputStream());
        return R.ok(tbEngineeringService.saveBatch(list), "成功导入 " + list.size() + " 条记录");
    }
}
